package com.sheep.pattern.constructor;

import java.lang.reflect.Type;
import java.util.stream.Stream;

/**
 * 建造者模式
 * 当 isRef 为 true 的时候，arg 表示 String 类型的 refBeanId，type 不需要设置；
 * 当 isRef 为 false 的时候，arg、type 都需要设置。
 * 请根据这个需求，完善 ConstructorArg 类。
 *
 * @author wangze
 * @date 2021-01-13 16:31.
 **/
public class ConstructorArg {
	private boolean isRef;
	private Class type;
	private Object arg;

	// 设为私有
	private ConstructorArg() {

	}
	// 静态内部类实现

	public static class Builder {
		private ConstructorArg constructorArg;

		public Builder() {
			constructorArg = new ConstructorArg();
		}

		public ConstructorArg build() {
			if (constructorArg.isRef) {
				if (constructorArg.arg == null) {
					throw new NullPointerException("参数为空");
				}
				if (!(constructorArg.arg instanceof String)) {
					throw new RuntimeException("必须为string 参数");
				}
				return constructorArg;
			}
			if (constructorArg.type == null || constructorArg.arg == null) {
				throw new NullPointerException("参数为空");
			}
			return constructorArg;
		}


		public Builder setRef(boolean ref) {
			constructorArg.isRef = ref;
			return this;
		}

		public Builder setType(Class type) {
			constructorArg.type = type;
			return this;
		}

		public Builder setArg(Object arg) {
			constructorArg.arg = arg;
			return this;
		}
	}

	public static void main(String[] args) {
		ConstructorArg arg = new ConstructorArg
				.Builder()
				.setArg("builder")
				.setRef(false).build();

	}
}
